package com.example.xihuashoujiduan.activity

import android.app.DatePickerDialog
import android.content.Intent
import android.view.LayoutInflater
import android.widget.Button
import android.widget.NumberPicker
import android.widget.Toast
import com.example.xihuashoujiduan.R
import com.example.xihuashoujiduan.base.BaseActivity
import com.example.xihuashoujiduan.databinding.ActivityBianjiziliaoBinding
import com.example.xihuashoujiduan.viewmodel.Mainviewmodel
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.lljjcoder.Interface.OnCityItemClickListener
import com.lljjcoder.bean.CityBean
import com.lljjcoder.bean.DistrictBean
import com.lljjcoder.bean.ProvinceBean
import com.lljjcoder.citywheel.CityConfig
import com.lljjcoder.style.citypickerview.CityPickerView

import java.util.Calendar

class BianjiziliaoActivity :BaseActivity<Mainviewmodel, ActivityBianjiziliaoBinding>(R.layout.activity_bianjiziliao){
    private lateinit var bottomSheetDialog: BottomSheetDialog
    var mPicker: CityPickerView = CityPickerView()
    override fun initView() {
        binding.touxiang.setOnClickListener {
            bottomSheetDialog = BottomSheetDialog(this)
            val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_paizhao, null)
            bottomSheetDialog.setContentView(view)


            bottomSheetDialog.show()
        }
        mPicker.init(this);
        binding.chengshi.setOnClickListener {
            chengshixuanzeqi()



        }
        binding.xiugainicheng.setOnClickListener {
            val intan=Intent(this,XiugainichnegActivity::class.java)
            startActivity(intan)
        }
        binding.xingbie.setOnClickListener {
            bottomSheetDialog = BottomSheetDialog(this)
            val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_xingbie, null)
            bottomSheetDialog.setContentView(view)
            bottomSheetDialog.show()
        }
        binding.shengrishezhi.setOnClickListener {
            bottomSheetDialog = BottomSheetDialog(this)
            val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_shengri, null)
            bottomSheetDialog.setContentView(view)
            val yearPicker = view.findViewById<NumberPicker>(R.id.yearPicker)
            val monthPicker = view.findViewById<NumberPicker>(R.id.monthPicker)
            val dayPicker = view.findViewById<NumberPicker>(R.id.dayPicker)
            val okButton = view.findViewById<Button>(R.id.okButton)
            val calendar = Calendar.getInstance()
            val currentYear = calendar.get(Calendar.YEAR)
            val currentMonth = calendar.get(Calendar.MONTH)
            val currentDay = calendar.get(Calendar.DAY_OF_MONTH)


            yearPicker.minValue = 1900
            yearPicker.maxValue = 2100
            yearPicker.value = currentYear
            monthPicker.minValue = 1
            monthPicker.maxValue = 12
            monthPicker.value = currentMonth + 1
            updateDayPicker(dayPicker, currentYear, currentMonth)
            dayPicker.value = currentDay

            yearPicker.setOnValueChangedListener { _, _, newYear ->
                val selectedMonth = monthPicker.value - 1
                updateDayPicker(dayPicker, newYear, selectedMonth)
            }

            monthPicker.setOnValueChangedListener { _, _, newMonth ->
                val selectedYear = yearPicker.value
                updateDayPicker(dayPicker, selectedYear, newMonth - 1)
            }
            okButton.setOnClickListener {
                val selectedYear = yearPicker.value
                val selectedMonth = monthPicker.value
                val selectedDay = dayPicker.value
                val selectedDate = "$selectedYear-$selectedMonth-$selectedDay"
                bottomSheetDialog.dismiss()
            }

            bottomSheetDialog.show()


        }


    }
    private fun chengshixuanzeqi(){

        //添加默认的配置，不需要自己定义，当然也可以自定义相关熟悉，详细属性请看demo
        val cityConfig = CityConfig.Builder().build()
        mPicker.setConfig(cityConfig)
        mPicker.setOnCityItemClickListener(object : OnCityItemClickListener() {
            override fun onSelected(
                province: ProvinceBean?,
                city: CityBean?,
                district: DistrictBean?
            ) {
                //省份province
                //城市city
                //地区district
                binding.chengshi.text=""+province+city+district

            }

            override fun onCancel() {

            }
        })

        mPicker.showCityPicker()
        //显示



    }

    private fun updateDayPicker(dayPicker: NumberPicker, year: Int, month: Int) {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR, year)
        calendar.set(Calendar.MONTH, month)
        val maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        dayPicker.minValue = 1
        dayPicker.maxValue = maxDays
    }



    override fun dataObservese() {

    }
}